/**
 *  state树
 *  {
 *    Player: {
 *      progress: '-',
 *       volume: 0,
 *       isPlay: false,
 *       leftTime: ''
 *    },
 *    Musics: {
 *      musicItem: MUSIC_LIST[0],
 *      musicList: MUSIC_LIST
 *    }

 *  }
 */
import { TOGGLE_PLAY, CHANGE_PROGRESS, CHANGE_VOLUME, LEFTTIME, PREV_MUSIC, NEXT_MUSIC, DefaultValues } from '../action/actions';
import { MUSIC_LIST } from '../config/musiclist';

// 初始值
const initialState = {
  // Player: {
    progress: DefaultValues.progress,
    volume: DefaultValues.volume,
    isPlay: DefaultValues.isPlay,
    leftTime: DefaultValues.leftTime,
  // },
  // Musics: {
    musicItem: MUSIC_LIST[0],
    musicList: MUSIC_LIST
  // }
};

function MusicPlayer(state = initialState, action) {
  switch (action.type) {
    // 切换播放
    case TOGGLE_PLAY:
      if(action.index == 1)
        return { ...state, isPlay: true }
      else
        return { ...state, isPlay: false }
    // 改变播放进度
    case CHANGE_PROGRESS:
      return { ...state, progress: action.prog }
    // 改变音量
    case CHANGE_VOLUME:
      return { ...state, volume: action.vu }
    case LEFTTIME:
      return { ...state, leftTime: action.time }
    // 上一曲
    case PREV_MUSIC:
      if(action.index - 1 >= 0)
        return { ...state, musicItem: MUSIC_LIST[action.index-1] }
      else
        return { ...state, musicItem: MUSIC_LIST[0] }
    // 下一曲
    case NEXT_MUSIC:
      if(action.index + 1 < MUSIC_LIST.length)
        return { ...state, musicItem: MUSIC_LIST[action.index+1] }
      else
        return { ...state, musicItem: MUSIC_LIST[action.index] }

    default:
      return state;

  }
}

export default MusicPlayer;
